package com.dminvoicemanager.adapters;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DM_ItemsList_Adapter {

	public static final String KEY_ITEMROWID = "_id";
	public static final String KEY_ITEMCODE = "ItemCode";
	public static final String KEY_ITEMNAME = "ItemName";
	public static final String KEY_ITEMAVGPRICE = "AvgPrice";
	public static final String KEY_ITEMBRAND = "SBrandCode";
	public static final String KEY_ITEMLOCCODE = "LocCode";
	public static final String KEY_ITEMQOH = "QOH";

	private static final String TAG = "DM_ItemsList_Adapter";
	private DatabaseHelper mDbHelperItems;
	private SQLiteDatabase mDb;

	private static final String DATABASE_NAME = "dminvoicemanager.db";
	private static final String SQLITE_TABLE_ITEMS = "fItems";
	private static final int DATABASE_VERSION = 6;
	
	private final Context mCtx;

	private static final String DATABASE_CREATE = "CREATE TABLE if not exists "
			+ SQLITE_TABLE_ITEMS + " (" + KEY_ITEMROWID
			+ " integer PRIMARY KEY autoincrement," + KEY_ITEMCODE + "," + KEY_ITEMNAME
			+ "," + KEY_ITEMAVGPRICE + "," + KEY_ITEMBRAND + "," + " UNIQUE ("
			+ KEY_ITEMCODE + "));";

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			//Log.w(TAG, DATABASE_CREATE);
			//db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE_ITEMS);
			onCreate(db);
		}
	}

	public DM_ItemsList_Adapter(Context ctx) {
		this.mCtx = ctx;
	}

	public DM_ItemsList_Adapter open() throws SQLException {
		mDbHelperItems = new DatabaseHelper(mCtx);
		mDb = mDbHelperItems.getWritableDatabase();
		return this;
	}

	public void close() {
		if (mDbHelperItems != null) {
			mDbHelperItems.close();
		}
	}

	public long createCountry(String code, String name, String continent,
			String region) {

		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_ITEMCODE, code);
		initialValues.put(KEY_ITEMNAME, name);
		initialValues.put(KEY_ITEMAVGPRICE, continent);
		initialValues.put(KEY_ITEMBRAND, region);

		return mDb.insert(SQLITE_TABLE_ITEMS, null, initialValues);
	}

	public boolean deleteAllCountries() {

		int doneDelete = 0;
		doneDelete = mDb.delete(SQLITE_TABLE_ITEMS, null, null);
		Log.w(TAG, Integer.toString(doneDelete));
		return doneDelete > 0;

	}

	public Cursor fetchItemsByName(String inputText) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			mCursor = mDb.query(SQLITE_TABLE_ITEMS, new String[] { KEY_ITEMROWID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE, KEY_ITEMBRAND }, null,
					null, null, null, null);

		} else {
			mCursor = mDb.query(true, SQLITE_TABLE_ITEMS, new String[] { KEY_ITEMROWID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE, KEY_ITEMBRAND }, KEY_ITEMNAME
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public Cursor fetchAllItems() {

		String query = "SELECT _id,ItemCode,ItemName,AvgPrice,SBrandCode FROM fItems";
		//Cursor mCursor = mDb.query();
		Cursor mCursor = mDb.rawQuery(query, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}
	public Cursor fetchItemsByNameSparm(String inputText, String userLocCode) throws SQLException {
		Log.w(TAG, inputText);
		Cursor mCursor = null;
		if (inputText == null || inputText.length() == 0) {
			/*mCursor = mDb.query(SQLITE_TABLE_ITEMS, new String[] { KEY_ITEMROWID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE, KEY_ITEMBRAND }, null,
					null, null, null, null);*/

			String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '" + userLocCode + "' ORDER BY OrdSeq ASC";
			//Cursor mCursor = mDb.query();
			mCursor = mDb.rawQuery(query, null);
		
		} else {
			/*mCursor = mDb.query(true, SQLITE_TABLE_ITEMS, new String[] { KEY_ITEMROWID,
					KEY_ITEMCODE, KEY_ITEMNAME, KEY_ITEMAVGPRICE, KEY_ITEMBRAND }, KEY_ITEMNAME
					+ " like '%" + inputText + "%'", null, null, null, null,
					null);*/
			String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '" + userLocCode + "' AND item.ItemName LIKE '%" + inputText + "%' ORDER BY OrdSeq ASC";
			//Cursor mCursor = mDb.query();
			mCursor = mDb.rawQuery(query, null);
		}
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	//Squry execute for Secondry Invoice
	public Cursor fetchAllItemsparm(String userLocCode) {

		String query = "SELECT item._id,item.ItemCode,item.ItemName,item.AvgPrice,item.SBrandCode,floc.LocCode,floc.ItemCode,floc.QOH FROM fItems item,fItemLoc floc WHERE item.ItemCode = floc.ItemCode AND floc.LocCode = '" + userLocCode + "' ORDER BY OrdSeq ASC";
		//Cursor mCursor = mDb.query();
		Cursor mCursor = mDb.rawQuery(query, null);

		if (mCursor != null) {
			mCursor.moveToNext();
		}
		return mCursor;
	}


}
